package com.mybatisdemo.controller;

import com.fazecast.jSerialComm.SerialPort;
import com.mybatisdemo.entity.IotMessage;
import com.mybatisdemo.service.IotMessageService;
import com.socket.SendAT;
import com.socket.SerialConsoleMonitor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/mybatisdemo/iot")
public class IotController {

    @Autowired
    IotMessageService iotMessageService;

    @Autowired
    SerialConsoleMonitor serialConsoleMonitor;

    @GetMapping("/queryiotall")
    public List<IotMessage> queryALL(){
        //进行业务处理，最后把结果返回给用户
        return iotMessageService.list();
    }

    @PostMapping("/saveiotall")
    public void saveiotall(IotMessage iotMessage){
        System.out.println("9999");
        //进行业务处理，最后把结果返回给用户
         iotMessageService.save(iotMessage);
    }

    @GetMapping("/kaiguan")
    public boolean kaiguan(boolean b){
        try {
            if(b == true){
                log.info("开-------------------------------");
                serialConsoleMonitor.openBuzzer();
            }else {
                log.info("关================================");
                serialConsoleMonitor.closeBuzzer();
            }
            return true;
        }catch (Exception e){
           e.printStackTrace();
        }
        return false;
    }

    @GetMapping("/tuoji")
    public boolean tuoji(int currentAngle){
        System.out.println("设置的角度是："+currentAngle);
        return false;
    }
}
